﻿Public Class produitTestListView
    Inherits System.Web.UI.Page
    Dim savePath As String


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not CType(HttpContext.Current.Session("client"), client).niveauAccess > 0 Then
            Response.Redirect("/membre/profil.aspx")
        End If
    End Sub

    Private Sub LinqDataSource3_Deleted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceStatusEventArgs) Handles LinqDataSource3.Deleted
        DropDownList1.DataBind()
        ListView1.DataBind()
    End Sub

    Private Sub LinqDataSource3_Inserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceStatusEventArgs) Handles LinqDataSource3.Inserted

        DropDownList1.DataBind()
        ListView1.DataBind()

    End Sub

    Private Sub LinqDataSource3_Updated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceStatusEventArgs) Handles LinqDataSource3.Updated

        DropDownList1.DataBind()
        ListView1.DataBind()
    End Sub

    Private Sub ListView2_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewInsertEventArgs) Handles ListView2.ItemInserting

        ' Specify the path on the server to
        ' save the uploaded file to.

        Dim FileUpload1 As FileUpload = CType(ListView2.InsertItem.FindControl("FileUpload1"), FileUpload)
        savePath = MapPath("../files/img/")

        ' Get the name of the file to upload.
        Dim fileName As String = Server.HtmlEncode(FileUpload1.FileName)

        ' Before attempting to save the file, verify
        ' that the FileUpload control contains a file.

        If (FileUpload1.HasFile) Then

            ' Get the extension of the uploaded file.
            Dim extension As String = System.IO.Path.GetExtension(fileName)

            ' Allow only files with .doc or .xls extensions
            ' to be uploaded.
            If (extension = ".jpg") Or (extension = ".jpeg") Or (extension = ".gif") Then

                Dim exist As Boolean = True

                Do While exist
                    fileName = IO.Path.GetFileNameWithoutExtension(fileName) + Now.Ticks.ToString() + extension
                    For Each files As String In IO.Directory.GetFiles(savePath)
                        If Not files = fileName Then
                            exist = False
                        End If
                    Next
                Loop

                savePath += fileName

                ' Call the SaveAs method to save the 
                ' uploaded file to the specified path.
                ' This example does not perform all
                ' the necessary error checking.               
                ' If a file with the same name
                ' already exists in the specified path,  
                ' the uploaded file overwrites it.
                FileUpload1.SaveAs(savePath)

                ' Notify the user that their file was successfully uploaded.
                UploadStatusLabel.Text = "Your file was uploaded successfully."

            Else
                ' Notify the user why their file was not uploaded.
                UploadStatusLabel.Text = "Your file was not uploaded because " + _
                                         "it does not have a .jpg or .jpeg extension."
            End If

        Else
            ' Notify the user that a file was not uploaded.
            UploadStatusLabel.Text = "You did not specify a file to upload."
        End If

        e.Values("image") = "/files/img/" + fileName
    End Sub

    Private Sub ListView2_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewUpdateEventArgs) Handles ListView2.ItemUpdating

        ' Specify the path on the server to
        ' save the uploaded file to.

        Dim FileUpload1 As FileUpload = CType(ListView2.EditItem.FindControl("FileUpload1"), FileUpload)
        savePath = MapPath("../files/img/")

        ' Get the name of the file to upload.
        Dim fileName As String = Server.HtmlEncode(FileUpload1.FileName)

        ' Before attempting to save the file, verify
        ' that the FileUpload control contains a file.

        If (FileUpload1.HasFile) Then

            ' Get the extension of the uploaded file.
            Dim extension As String = System.IO.Path.GetExtension(fileName)

            ' Allow only files with .doc or .xls extensions
            ' to be uploaded.
            If (extension = ".jpg") Or (extension = ".jpeg") Or (extension = ".gif") Then

                Dim exist As Boolean = True

                Do While exist
                    fileName = IO.Path.GetFileNameWithoutExtension(fileName) + Now.Ticks.ToString() + extension
                    For Each files As String In IO.Directory.GetFiles(savePath)
                        If Not files = fileName Then
                            exist = False
                        End If
                    Next
                Loop

                savePath += fileName

                ' Call the SaveAs method to save the 
                ' uploaded file to the specified path.
                ' This example does not perform all
                ' the necessary error checking.               
                ' If a file with the same name
                ' already exists in the specified path,  
                ' the uploaded file overwrites it.
                FileUpload1.SaveAs(savePath)

                ' Notify the user that their file was successfully uploaded.
                UploadStatusLabel.Text = "Your file was uploaded successfully."

            Else
                ' Notify the user why their file was not uploaded.
                UploadStatusLabel.Text = "Your file was not uploaded because " + _
                                         "it does not have a .jpg or .jpeg extension."
            End If

        Else
            ' Notify the user that a file was not uploaded.
            UploadStatusLabel.Text = "You did not specify a file to upload."
        End If

        e.NewValues("image") = "/files/img/" + fileName
    End Sub
End Class